package com.example.business;

import com.example.data.mapper.UserMapper;
import com.example.data.model.User;
import com.example.utils.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * Created by hldev on 16-12-9.
 */
@Service
public class UserService {

    @Autowired
    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    public User findOne(Integer id) {
        return userMapper.findOne(id);
    }

    public User findByUserName(String userName) {
        return userMapper.findByUserName(userName);
    }

    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Transactional
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    public void createUser(User user) {
        user.setPassWord(Encrypt.encryptStrWithMD5(user.getPassWord() + user.getUserName()));
        userMapper.createUser(user);
    }

    public void deleteUser(Integer id) {
        userMapper.delete(id);
    }
}